<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="性别" prop="gender">
        <el-select v-model="queryParams.gender" placeholder="性别" clearable size="small">
          <el-option
            v-for="dict in genderOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="driverMobile">
        <el-input
          v-model="queryParams.driverMobile"
          placeholder="请输入司机手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否可用" prop="isEnable">
        <el-select v-model="queryParams.isEnable" placeholder="是否可用" clearable size="small">
          <el-option
            v-for="dict in driverStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>

      </el-form-item>

      <el-form-item label="是否接单" prop="orderSwitch">
        <el-select v-model="queryParams.orderSwitch" placeholder="是否接单" clearable size="small">
          <el-option
            v-for="dict in enableOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>

      </el-form-item>

      <el-form-item label="代驾经验" prop="experience">
        <el-select v-model="queryParams.experience" placeholder="代驾经验" clearable size="small">
          <el-option
            v-for="dict in experienceOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="注册时间" prop="gmtTime">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['driver:driver:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['driver:driver:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['driver:driver:remove']"
        >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="driverList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="头像" align="center" prop="driverAvator">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="80"  :src="ossPreview+ scope.row.driverAvator"></el-avatar>
        </template>

      </el-table-column>
      <el-table-column label="姓名" align="center" prop="driverName"/>
      <el-table-column label="性别" :formatter="genderFormat" align="center" prop="gender"/>
      <el-table-column label="手机号" align="center" prop="driverMobile"/>
      <el-table-column label="身份证号" align="center" prop="idCard"/>
      <el-table-column label="是否可用" :formatter="enableFormat" align="center" prop="isEnable"/>
      <el-table-column label="是否接单" :formatter="switchFormat" align="center" prop="orderSwitch"/>
      <el-table-column label="代价费" align="center" prop="agentFee"/>
      <el-table-column label="代驾经验" :formatter="experienceFormat" align="center" prop="experience"/>
      <el-table-column label="坐标" align="center" prop="coordinate"/>
      <el-table-column label="位置" align="center" prop="locPoi"/>
      <el-table-column label="注册时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown"  @command="handleCommand">
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['driver:driver:edit']"
                >修改
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['driver:driver:remove']"
                >删除
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-s-grid"
                  @click="handleOrder(scope.row)"
                  v-hasPermi="['order:order:list']"
                >订单
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 添加或修改代驾司机对话框 -->
    <el-dialog :title="title" :visible.sync="open" :destroy-on-close="destroy" width="800px" v-dialogdrag append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="司机头像" prop="driverAvator">
          <driver-avatar :user="form" @driverAvatorEvent="emitdriverAvator" :key="(new Date().getTime())"></driver-avatar>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请输入性别" clearable size="small">
            <el-option
              v-for="dict in genderOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="form.driverName" placeholder="请输入司机姓名"/>
        </el-form-item>

        <el-form-item label="手机号" prop="driverMobile">
          <el-input v-model="form.driverMobile" placeholder="请输入司机手机号"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="是否可用" prop="isEnable">
          <el-select v-model="form.isEnable" placeholder="请输入是否可用" clearable size="small">
            <el-option
              v-for="dict in driverStateOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否接单" prop="orderSwitch">
          <el-select v-model="form.orderSwitch" placeholder="请输入是否接单" clearable >
            <el-option
              v-for="dict in enableOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="代价费" prop="agentFee">
          <el-input-number v-model="form.agentFee" :precision="2" :step="1" :min="1" :max="100"></el-input-number>
        </el-form-item>
        <el-form-item label="代驾经验" prop="experience">
          <el-select v-model="form.experience" placeholder="请输入代驾经验" clearable size="small">
            <el-option
              v-for="dict in experienceOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="位置" prop="experience">
          <amap :default-value="postForm.detailedAddress" @getPosition="getPosition" ></amap>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 订单 -->
    <el-dialog :title="title" :visible.sync="orderOpen" :destroy-on-close="destroy" width="1300px" v-dialogdrag append-to-body>
      <order-list :driverId="driverId" :key="(new Date().getTime())"></order-list>
    </el-dialog>


  </div>
</template>

<script>
  import {listDriver, getDriver, delDriver, addDriver, updateDriver} from "@/api/driver/driver";
  import defaultSettings from '@/settings'
  import driverAvatar from "../../driver/driver/driverAvatar";
  import orderList from '../../order/order/index'
  import Amap from '@/components/AMap/index'
  export default {
    name: "Driver",
    props: {
      stationId: {
        type: Number
      }
    },
    components: {driverAvatar,orderList,Amap},
    data() {
      return {
        driverId:null,
        ossPreview: defaultSettings.ossPreview,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 代驾司机表格数据
        driverList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        orderOpen: false,
        stationOpen: false,
        destroy: true,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          gender: undefined,
          driverName: undefined,
          driverMobile: undefined,
          idCard: undefined,
          isEnable: undefined,
          orderSwitch: undefined,
          password: undefined,
          agentFee: undefined,
          experience: undefined,
          gmtTime: undefined,
          stationId:this.stationId
        },
        // 性别数据字典
        genderOptions: [],
        // 状态数据字典
        enableOptions: [],
        driverStateOptions: [],
        // 驾驶经验数据字典
        experienceOptions: [],
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        // 日期范围
        dateRange: [],
        text:{
          formattedAddress: "郑州市人民政府",
          longitude: 113.625351,
          latitude: 34.746303
        },
        postForm: {
          detailedAddress: '',
          longitude: '',
          latitude: ''
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_user_sex").then(response => {
        this.genderOptions = response.data;
      });
      this.getDicts("driver_state").then(response => {
        this.driverStateOptions = response.data;
      });
      this.getDicts("sys_is_enable").then(response => {
        this.enableOptions = response.data;
      });
      this.getDicts("driving_experience").then(response => {
        this.experienceOptions = response.data;
      });
    },
    methods: {
      /** 查询代驾司机列表 */
      getList() {
        this.loading = true;
        listDriver(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.driverList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      emitChooseStation() {
        this.stationOpen = false;
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          gender: undefined,
          driverName: undefined,
          driverAvator: undefined,
          driverMobile: undefined,
          idcardFront: undefined,
          idcardBack: undefined,
          idCard: undefined,
          isEnable: undefined,
          orderSwitch: undefined,
          openId: undefined,
          password: undefined,
          agentFee: undefined,
          experience: undefined,
          coordinate: undefined,
          locPoi: undefined,
          gmtTime: undefined,
          locPoi: "郑州市人民政府",
          coordinate: "113.625351,34.746303",
          stationId:this.stationId
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加代驾司机";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getDriver(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改代驾司机";
          var coors = this.form.coordinate.split(",");
          this.postForm.longitude = coors[0];
          this.postForm.latitude = coors[1];
          this.postForm.detailedAddress = this.form.locPoi;
        });
      },

      /** 修改按钮操作 */
      handleOrder(row) {
        this.driverId = row.id;
        this.orderOpen = true;
        this.title = "代驾司机订单";
      },

      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.coordinate = this.postForm.longitude + "," + this.postForm.latitude;
            this.form.locPoi = this.postForm.detailedAddress;
            if (this.form.id != undefined) {
              updateDriver(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addDriver(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除代驾司机编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delDriver(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      getPosition({ address, lat, lng, name }) {
        this.postForm.detailedAddress = address;
        this.postForm.longitude = String(lng);
        this.postForm.latitude = String(lat);
      },
      emitdriverAvator(data){
        this.form.driverAvator = data;
      },
      genderFormat(row, column) {
        return this.selectDictLabel(this.genderOptions, row.gender);
      },
      enableFormat(row, column) {
        return this.selectDictLabel(this.driverStateOptions, row.isEnable);
      } ,
      switchFormat(row, column) {
        return this.selectDictLabel(this.enableOptions, row.orderSwitch);
      } ,
      experienceFormat(row, column) {
        return this.selectDictLabel(this.experienceOptions, row.experience);
      }
    }
  };
</script>
